#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
#
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM Add_Delta_Days Today);
use Text::CSV;

use avsCommon;


dbsetup();

my ($content, $response, $request, $url);
my ($period, $custid);
my ($runyear, $runmonth, $runday);

# create user agent, set cookies
my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->timeout(300);
$ua->cookie_jar( {} );

# loop through all the colleges
do {
	getLogin ();
	
	($runyear, $runmonth, $runday) = @enddate;
	
	# run once for each year, wilson returns all months to date in year. typicall only run for one month.
	while ($runyear >= $begyear) {
		getStats ();
		$runyear--;
		$runmonth = 12;
	}

} while $sthlogin->fetch;

dbshutdown();

exit();

sub getLogin {
	# setup cookies
	$url = 'http://www.hwwstats.com/ng/';
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

	#login
	$url = 'http://www.hwwstats.com/ng/loginVerification.cfm';
	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("Userid=$username&Password=$password&custid=&LoginType=UserPwd");

	$response = $ua->request($request);
	if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
		die "#error:$0: POST report failed $url : " . $response->status_line;
	};
	
	# get the customer id
	$url='http://www.hwwstats.com/ng/' .  $response->header('location');
	$url=~ /custid=(?<custid>\d*)/;
	$custid=$+{custid};

	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

	#setup the dr1 type of report
	$url = 'http://www.hwwstats.com/ng/statsIndex.cfm?page=dr1';
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

}

sub getStats {
	#ask for the report
	$url = 'http://www.hwwstats.com/ng/statsIndex.cfm?page=dr1';
	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("custid=$custid&ACCTTYPE=shipto&yr=$runyear");
	$response = $ua->request($request);
	if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
		die "#error:$0: POST report failed $url : " . $response->status_line;
	};

	$content = $response->decoded_content;

	# save contents for possible debugging
	savecontent('Wilson', "$collegeid", $content);

	# we now have the report in html format.
	# take the html and reformat for easier data extraction
	$content =~ s/\s\s//g; # remove spaces/cr lf
	$content =~ s/<\/tr>/\n/g; # add cr
	$content =~ s/<\/td>/~/g; # add splitable character
	$content =~ s/<.*?>//g; # remove html
	$content =~ s/ ~/~/g; # remove extra space in datasources
	$content =~ s/\t//g; # remove tabs 	wilson changed something 1-2009

	# find the actual data table
	#	$content = substr($content, index($content, 'Metrics~') + 8);
	#	my @contents = split("\n",$content);
	# wilson changed something 1-2009
	$content =~ /Metrics~(?<data>.*?)\n~\n/s;
	my @contents = split("\n",$+{data});
	
	# grab the dates off of the header lines.
	my @periods = ('','','','');	
	my $begperiod = myperiod($begyear, $begmonth);
	my $endperiod = myperiod($runyear, $runmonth);
	my $begperiodndx = 4;
	my $endperiodndx = 4;

	my @months = split('~', shift(@contents));
	foreach my $months (@months[0..$#months-1]) {
		my $period = myperiod($runyear,$months);
		$begperiodndx++ if ($period lt $begperiod);
		$endperiodndx++ if ($period lt $endperiod);
		push @periods, $period;
	}
	$begperiodndx-- if $begperiodndx == 16;	# prevent multiyear problem
	$endperiodndx-- if $endperiodndx == 16;	# prevent multiyear problem

	# init the counters needed
	my $datasource='';
	my @zfill=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	my @sessions=@zfill;
	my @searches=@zfill;

	# go through each line of data. searches and sessions are on seperate lines so
	# must save and break
	foreach my $contents (@contents) {

		# ignore blank lines
		next if ($contents eq '~'||$contents eq ''); 

		my @columns = split('~',$contents);

		# update data on datasource break
		if ($columns[0] ne $datasource) {
			if ($datasource ne '') {
				foreach my $i ($begperiodndx..$endperiodndx) {
					my $session = $sessions[$i];
					my $search  = $searches[$i];
					print "$vendorid $collegeid $periods[$i] $datasource $session $search\n";
					$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $session, 1)
						or die '#error: sthstats execute 1 failed' . $dbhstats->errstr;
					$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $search, 2 )
						or die '#error: sthstats execute 2 failed' . $dbhstats->errstr;
				}
			}

			$datasource = $columns[0];
			@sessions=@zfill;
			@searches=@zfill;

		}

		# determine data type and save in correct month
		if ($columns[3] eq 'Sessions') {
			@sessions = @columns;
		}
		else {
			@searches = @columns;
		}
	}

	if ($datasource ne '') {
		foreach my $i ($begperiodndx..$endperiodndx) {
			my $session = $sessions[$i];
			my $search  = $searches[$i];
			print "$vendorid $collegeid $periods[$i] $datasource $session $search\n";
			$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $session, 1)
						or die '#error: sthstats execute 1.2 failed' . $dbhstats->errstr;
			$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $search, 2 )
						or die '#error: sthstats execute 2.2 failed' . $dbhstats->errstr;
		}
	}
	else {
		print "#error:$0: blank datasource?\n";
	}
}